package com.alpha.eceasy.mall.service.impl;

import com.alpha.eceasy.exception.BizException;
import com.alpha.eceasy.extension.BaseFormInfo;
import com.alpha.eceasy.extension.BaseServiceImpl;
import com.alpha.eceasy.mall.api.dto.MallCommodityDTO;
import com.alpha.eceasy.mall.api.dto.MallCommoditySpecificationDTO;
import com.alpha.eceasy.mall.api.entity.MallCommodity;
import com.alpha.eceasy.mall.api.entity.MallCommoditySpecification;
import com.alpha.eceasy.mall.api.entity.MallCustomerCollect;
import com.alpha.eceasy.mall.api.vo.MallCommoditySpecificationVO;
import com.alpha.eceasy.mall.api.vo.MallCommodityVO;
import com.alpha.eceasy.mall.client.HWAdminApiClient;
import com.alpha.eceasy.mall.client.ResponseData;
import com.alpha.eceasy.mall.mapper.MallCommoditySpecificationMapper;
import com.alpha.eceasy.mall.service.MallCommoditySpecificationService;
import com.alpha.eceasy.mall.service.MallCustomerCollectService;
import com.alpha.eceasy.mall.util.SecurityUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Optional;

/**
 * @author huangpengfei
 */
@Service
public class MallCommoditySpecificationServiceImpl extends BaseServiceImpl<MallCommoditySpecificationMapper, MallCommoditySpecification>
        implements MallCommoditySpecificationService {
    @Resource
    private MallCommodityServiceImpl commodityService;
    @Resource
    private MallCustomerCollectServiceImpl customerCollectService;
    @Resource
    private ProductServiceImpl productService;
    @Resource
    private HWAdminApiClient hwAdminApiClient;

    @Override
    public IPage<MallCommoditySpecificationVO> getPage(Page<MallCommoditySpecification> page, MallCommoditySpecification entity) {
        return this.page(page, Wrappers.<MallCommoditySpecification>lambdaQuery(entity)
                        .orderByDesc(BaseFormInfo::getCreateTime))
                .convert(this::getCommoditySpecificationVO);
    }

    @Override
    public MallCommoditySpecificationVO getVOByGuid(Serializable guid) {
        MallCommoditySpecificationVO vo = getCommoditySpecificationVO(super.getByGuid(guid));
        Optional.ofNullable(customerCollectService.getOne(Wrappers.<MallCustomerCollect>lambdaQuery()
                        .eq(MallCustomerCollect::getCommoditySpecificationGuid, guid)
                        .eq(MallCustomerCollect::getCustomerGuid, SecurityUtils.getUserGuid())))
                .ifPresent(vo::setCustomerCollect);
        Optional.ofNullable(vo.getDictCurrencyCode())
                .map(code -> hwAdminApiClient.getCurrencyByCode(code))
                .map(ResponseData::getData)
                .ifPresent(vo::setDictCurrency);
        return vo;
    }

    @Override
    public MallCommoditySpecificationVO getCommoditySpecificationVO(MallCommoditySpecification commoditySpecification) {
        MallCommoditySpecificationVO vo = new MallCommoditySpecificationVO();
        MallCommodityVO commodityVO = commodityService.getByGuid(commoditySpecification.getCommodityGuid());
        BeanUtils.copyProperties(commodityVO, vo);
        BeanUtils.copyProperties(commoditySpecification, vo);
        vo.setSpecificationCoverList(commoditySpecification.getSpecificationCoverList());
        return vo;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveAndCreateCommodity(MallCommoditySpecificationDTO dto) {
        String commodityGuid = commodityService.save(new MallCommodityDTO(dto));
        MallCommoditySpecification specification = new MallCommoditySpecification(dto);
        specification.setInfoProductGuid(dto.getInfoProductGuid());
        specification.setCommodityGuid(commodityGuid);
        if (!productService.updateStatusIdByGuid(dto.getInfoProductGuid(), (byte) 4)) {
            throw new BizException("商品发布失败");
        }
        return super.save(specification);
    }

    @Override
    @Transactional
    public boolean removeByGuid(Serializable guid) {
        MallCommoditySpecification commoditySpecification = super.getByGuid(guid);
        if (!productService.updateStatusIdByGuid(commoditySpecification.getInfoProductGuid(), (byte) 1)) {
            throw new BizException("商品删除失败");
        }
        return super.removeByGuid(guid);
    }
}
